<?php
/**
 * Jishupu-Laravel-Vue-Admin
 * @Author:技术铺<jishupu@qq.com>
 * @Date: 2021/11/11
 */

namespace App\Http\Requests;

use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

/**
 * 用户模块表单验证
 * Class UserRequest
 * @package App\Http\Requests
 */
class UserRequest extends BaseRequest
{

    /**
     * 注入表单请求验证时自动校验
     *
     * @var bool
     */
    public $autoValidate = false;

    /**
     * 用户是否有权操作，true是，false否
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 字段验证规则
     *
     * @return array
     */
    public function rules()
    {
        $user = $this->user;
        return [
            'id' => 'required|integer',
            'created_user_id' => 'required|integer',
            'name' => 'required|min:2|max:30',
            'username' => ['required', 'min:3', 'max:50', 'regex:/^[a-zA-Z]{1}[a-zA-Z0-9]+$/', Rule::unique('user')->ignore($user)->where(function ($query) {
                return $query->where('deleted_at', null);
            })],
            'password' => [(!$user ? 'required' : 'present'), 'confirmed'],
            'avatar_image' => [
                'nullable',
                function ($attribute, $value, $fail) {
                    AttachmentRequest::validity(__("user.avatar_image"), $value, $fail, 1);
                }
            ],
            'role_ids' => 'required|array',
            'phone_number' => ['nullable','max:30', Rule::unique('user')->ignore($user)->where(function ($query) {
                return $query->where('deleted_at', null);
            })],
            'email' => ['nullable','email', Rule::unique('user')->ignore($user)->where(function ($query) {
                return $query->where('deleted_at', null);
            })],
            'status' => 'required|boolean',
            'description' => 'nullable|max:200'
        ];
    }

    /**
     * 提示信息
     * @return array
     */
    public function messages()
    {
        return [
        ];
    }

    /**
     * 定义场景
     * @var \string[][]
     */
    public $scenes = [
        'store' => ['created_user_id', 'name', 'username', 'password', 'description', 'phone_number', 'email', 'avatar_image', 'role_ids', 'status'],
        'update' => ['name', 'username', 'password', 'description', 'phone_number', 'email', 'avatar_image', 'role_ids', 'status'],
        'destroy' => ['id'],
        'updateStatus' => ['status'],
        'updateProfile' => ['name', 'username', 'password', 'description', 'phone_number', 'email', 'avatar_image']
    ];

    /**
     * 验证错误的自定义属性
     *
     * @return array
     */
    public function attributes()
    {
        return [
            'id' => __('common.id'),
            'name' => __('user.name'),
            'username' => __('user.username'),
            'password' => __('user.password'),
            'phone_number' => __('user.phone_number'),
            'email' => __('user.email'),
            'avatar_image' => __('user.avatar_image'),
            'role_ids' => __('user.role_ids'),
            'status' => __('common.status'),
            'description' => __('user.description'),
        ];
    }

    /**
     * 验证前
     *
     * @return void
     */
    protected function prepareForValidation()
    {
        $this->offsetSet('created_user_id', Auth::id());
        if (!isset($this->avatar_image) || empty($this->avatar_image)) $this->offsetSet('avatar_image', 0);
        $this->offsetSet('password', trim($this->password));
    }


}
